package com.zsh.wanandroid.ui.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.bean.ArticleBean;
import com.zsh.wanandroid.bean.ArticleTags;
import com.zsh.wanandroid.bean.NavigationBean;
import com.zsh.wanandroid.impl.AdapterItemClickListener;
import com.zsh.wanandroid.utils.UIUtils;

import java.util.List;

/**
 * @author zsh
 * @date 2020/3/19.
 * 功能：导航右侧adapter
 */

public class NavigationRightAdapter extends BaseQuickAdapter<NavigationBean<ArticleBean<ArticleTags>>, BaseViewHolder> {

    public NavigationRightAdapter(@LayoutRes int layoutResId, @Nullable List<NavigationBean<ArticleBean<ArticleTags>>> data) {
        super(layoutResId, data);
    }

    public NavigationRightAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    public NavigationRightAdapter(@Nullable List<NavigationBean<ArticleBean<ArticleTags>>> data) {
        super(data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final NavigationBean<ArticleBean<ArticleTags>> item) {
        helper.setText(R.id.tvTitle, item.getName());
        List<ArticleBean<ArticleTags>> articles = item.getArticles();
        TagFlowLayout tagFlowLayout = helper.getView(R.id.tagFlowLayout);
        tagFlowLayout.setAdapter(new TagAdapter<ArticleBean<ArticleTags>>(articles) {
            @Override
            public View getView(FlowLayout parent, final int position, ArticleBean<ArticleTags> bean) {
                TextView view = (TextView) UIUtils.getView(R.layout.flow_view);
                view.setText(bean.getTitle());
                return view;
            }
        });

        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                adapterItemClickListener.onItemClick(item, position);
                return true;
            }
        });
    }

    public interface AdapterItemClickListener {
        void onItemClick(NavigationBean<ArticleBean<ArticleTags>> item, int position);
    }

    //监听接口
    public AdapterItemClickListener adapterItemClickListener; //定义监听接口对象

    public void setAdapterItemClickListener(AdapterItemClickListener adapterItemClickListener) {
        this.adapterItemClickListener = adapterItemClickListener;
    }
}
